﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ECSE6770.Models;

namespace ECSE6770.Controllers
{
    [RequireHttps]
    [Authorize(Roles = "Admin")]
    public class AdminController : BaseAdminController
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult NewUser()
        {
            return View();
        }

        public ActionResult EditTicker()
        {
            return View(Db.TickerItems.ToList());
        }

        [HttpPost]
        public ActionResult EditTicker(string symbols)
        {
            Db.TickerItems.RemoveWhere(t => true);
            foreach (string s in symbols.Split(','))
                Db.TickerItems.Add(new TickerItem { Symbol = s, Name = String.Empty });
            Db.SaveChanges();
            ViewBag.Message = "Ticker information has been updated.";
            return View(Db.TickerItems.ToList());
        }

        [HttpPost]
        public ActionResult NewUser(AdminNewUserModel model)
        {
            if (ModelState.IsValid && NonExistantUser(model.UserName))
            {
                string password = Membership.GeneratePassword(8, 2);
                Membership.CreateUser(model.UserName, password, model.Email);
                Db.UserInterops.Add(new UserInterop { UserName = model.UserName });
                Db.SaveChanges();
                NotificationManager.NotifyRegistration(model.UserName, password, model.Email);

                //store some info before redirecting
                TempData["EditProfileParams"] = new EditProfileParams { Title = "Enter Contact Information", LeftContent = this.LeftContent, BodyCss = this.BodyCss };
                return RedirectToAction("EditProfile", "Account", new { id = model.UserName });
            }
            else
                return View();
        }

        public ActionResult Delete(string id)
        {
            UserInterop interop = Db.UserInterops.First(u => u.UserName == id);

            interop.ModulesTaught.Clear();
            interop.PurchasedProducts.Clear();
            interop.PastOrders.Clear();
            Db.UserInterops.Remove(interop);
            Db.Feedback.RemoveWhere(f => f.UserInteropId == interop.Id);
            Db.SaveChanges();

            Membership.DeleteUser(id, true);
            return RedirectToAction("UserList");
        }

        public ActionResult UserList()
        {
            return View(Membership.GetAllUsers().OfType<MembershipUser>());
        }

        public ActionResult OrderHistory()
        {
            return View(OrderHistoryManager.AllOrders());
        }

        private bool NonExistantUser(string user)
        {
            if (Membership.FindUsersByName(user).Count > 0)
            {
                ModelState.AddModelError("already registered", String.Format("The user name {0} already exists. Please choose a different name.", user));
                return false;
            }
            return true;
        }
    }
}
